#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
	PROJECT pyevocomm -

	APP Parla

	MODULE	model

	SITE DESCRIPTION http://code.google.com/p/pyevocomm/ 

	@copyright: 2012 by archetipo <alessio.gerace@gmail.com>
	@license: New BSD License, see COPYING for details.
"""





import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..'))

# stdlib
import copy
import random
import weakref
from cocos.sprite import Sprite
import pyglet
import cocos
from pyglet.gl import *
from pyglet import gl, font
from cocos.layer import Layer, ColorLayer
from cocos.scene import Scene
from cocos.director import director
from cocos.actions import *

# tetrico related
from constants import *
from status import status
from editor import Editor
from button import Buttons

__all__ = ['Model']

#
# Model (of the MVC pattern)
#

class Model( pyglet.event.EventDispatcher ):

	def __init__(self):
		super(Model,self).__init__()

		# grid
		self.map = {}
		self.currspeech=''
		self.text=''
		self.actions=''
		status.init()
		self.ed=Editor()
		self.b=Buttons()
		self.refresh()
		
	def start( self ):
		pass

	def set_controller( self, ctrl ):
		self.ctrl = weakref.ref( ctrl )
	
	def select(self,val):
		if status.colsel==0:
			status.selcol()
		elif status.colsel==1:
			status.selline()
		self.refresh()
		self.action()
		self.dispatch_event("on_select")
	def close(self,val):
		print val
		self.refresh()
		
	def step(self,val):
		if status.linesel==0:
			if status.colsel==0:
				status.step_col()
			elif status.colsel==1:
				status.resetline()
				status.step_line()
		else:
			status.reset()
		self.refresh()
		self.dispatch_event("on_step")

	def insel(self,val):
		if status.colsel==0:
			status.inselcol()
		elif status.colsel==1:
			status.inselline()
		self.refresh()
		self.dispatch_event("on_inselect")
	
	def lost(self,val):
		if status.colsel==0:
			status.resetcol()
		elif status.colsel==1:
			status.resetline()
		self.refresh()

	def rigth(self,val):
		print val
		self.refresh()

	def left(self,val):
		print val
		self.refresh()

	def single(self,val):
		print val
		self.refresh()

	def lang(self,val):
		print val
		self.refresh()
		
	def menu(self,val):
		status.setmenu()
		self.refresh()
		self.dispatch_event("on_menu")
		status.selcol()
		
	def refresh(self):
		self.b.refresh()
		self.currspeech=status.get_currspeech()
	def action(self):
		""" Function doc
	
		@param PARAM: DESCRIPTION
		@return RETURN: DESCRIPTION
		"""
		if status.linesel==1:
			self.actions=status.get_curraction()
			if self.actions=='spazio':
				if self.ed.inizial:self.ed.write(' ')
			elif self.actions=='cancella':
				if self.ed.inizial:self.ed.delete(self.text[-1:])
			elif self.actions=='rileggi':
				self.currspeech=self.ed.document.text
				self.dispatch_event("on_reload")
			elif self.actions=='scrivi':
				status.setlav()
				self.refresh()
			else:
				self.text+=self.actions
				self.controllaEditor()
			
			
	def controllaEditor(self):
		if self.ed.inizial==False and (self.text<>''):
			self.ed.init(self.actions)
		elif self.text=='':
			self.ed.inizial=False
		else:
			self.ed.write(self.actions)
			
	def draw(self):
		if self.text<>'':
			if self.ed.inizial:self.ed.draw()
		self.b.draw()
		
		




Model.register_event_type('on_select')
Model.register_event_type('on_close')
Model.register_event_type('on_step')
Model.register_event_type('on_inselect')
Model.register_event_type('on_lost')
Model.register_event_type('on_right')
Model.register_event_type('on_left')
Model.register_event_type('on_single')
Model.register_event_type('on_lang')
Model.register_event_type('on_menu')
Model.register_event_type('on_reload')
